#include "Client.h"


Client::Client(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ClientClass())
{
    ui->setupUi(this);

    m_httpClient = QSharedPointer<QNetworkHttpClient>::create("127.0.0.1", 8080);

    connect(ui->pushButton_send, &QPushButton::clicked, this, &Client::slot_sendMessage);
    connect(ui->pushButton_send_post, &QPushButton::clicked, this, &Client::slot_sendMessagePost);

    //initTCPClient();
}

Client::~Client()
{
    m_clientSocket->deleteLater();
    delete ui;
}


void Client::initTCPClient()
{
    m_clientSocket = new QTcpSocket(this);
    connect(m_clientSocket, &QTcpSocket::connected, this, &Client::slot_newConnection);
    connect(ui->pushButton_TCPSend, &QPushButton::clicked, this, &Client::slot_sendData);

    m_clientSocket->connectToHost("127.0.0.1", 8081);
}

void Client::slot_newConnection()
{
    connect(m_clientSocket, &QTcpSocket::readyRead, this, &Client::slot_getDataFromServer);
    connect(m_clientSocket, &QTcpSocket::disconnected, this, &Client::slot_disconnected);
}

void Client::slot_sendData()
{
    QString data = "#" + ui->lineEdit_TCPMessage->text() + "#";


    if (m_clientSocket->state() == QAbstractSocket::ConnectedState) {
        m_clientSocket->write(data.toUtf8());
        m_clientSocket->flush();
    }
}

void Client::slot_disconnected()
{
    qDebug() << "未与TCPserver连接";
}

void Client::slot_getDataFromServer()
{
    if (m_clientSocket->bytesAvailable()) {
        QByteArray data = m_clientSocket->readAll();
        static QByteArray buffer;
        buffer.append(data);

        while (buffer.contains('#')) {
            int startIdx = buffer.indexOf('#');
            int endIdx = buffer.indexOf('#', startIdx + 1);

            if (startIdx != -1 && endIdx != -1) {
                QByteArray message = buffer.mid(startIdx, endIdx - startIdx + 1);
                QString messageStr = QString::fromUtf8(message);
                qDebug() << "收到数据:" << messageStr;
                buffer.remove(0, endIdx + 1);
                ui->label_rspMessgae->setText(messageStr);
            }
            else {
                break;
            }
        }
    }
}



void Client::slot_sendMessagePost()
{
    QJsonObject json;
    json["str1"] = ui->lineEdit_message_post->text();
    json["str2"] = ui->lineEdit_message_post_2->text();
    QString jsonStr = QJsonDocument(json).toJson(QJsonDocument::Compact);

    QSharedPointer<ResponseContent> rsp = m_httpClient->postJsonSync("/api/postA", jsonStr, 1500, 1);
    if (rsp != nullptr) {
        if (200 == rsp->StatusCode) {
            QJsonParseError error;
            const QJsonDocument& document = QJsonDocument::fromJson(rsp->BodyContent.toUtf8(), &error);
            if (QJsonParseError::NoError == error.error) {
                const QJsonObject& programmerObject = document.object();
                QString str = programmerObject.value("str").toString();

                ui->label_rspMessgae->clear();
                ui->label_rspMessgae->setText(rsp->BodyContent);
            }
        }
        else {
            ui->label_rspMessgae->clear();
            ui->label_rspMessgae->setText("请求异常");
        }
    }
}

void Client::slot_sendMessage()
{
    QString msg = ui->lineEdit_message->text();
    QString msg2 = ui->lineEdit_message_2->text();
    QString path = QString("/api/getA?str1=%1&str2=%2").arg(msg).arg(msg2);
    QSharedPointer<ResponseContent> rsp = m_httpClient->getJsonASync(path, 1500, 1);
    if (rsp != nullptr) {
        if (rsp->StatusCode == 200) {
            ui->label_rspMessgae->clear();
            ui->label_rspMessgae->setText(rsp->BodyContent);
        }
        else {
            ui->label_rspMessgae->clear();
            ui->label_rspMessgae->setText("请求异常");
        }
    }   
}



